﻿using SD.Infrastructure.WPF.Caliburn.Aspects;
using SD.Infrastructure.WPF.Caliburn.Base;
using SD.Infrastructure.WPF.Commands;
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows;
using System.Windows.Input;

namespace SlamDunk.MES.Client.ViewModels.MaterialBarcode
{
    /// <summary>
    /// 扫码视图模型
    /// </summary>
    public class ScanViewModel : ScreenBase
    {
        #region # 字段及构造器

        /// <summary>
        /// 依赖注入构造器
        /// </summary>
        public ScanViewModel()
        {

        }

        #endregion

        #region 属性

        #region 条码编号 —— string BarcodeNo
        /// <summary>
        /// 条码编号
        /// </summary>
        [DependencyProperty]
        public string BarcodeNo { get; set; }
        #endregion

        #region 条码编号列表 —— ObservableCollection<string> BarcodeNos
        /// <summary>
        /// 条码编号列表
        /// </summary>
        [DependencyProperty]
        public ObservableCollection<string> BarcodeNos { get; set; }
        #endregion

        #region 只读属性 - 扫码命令 —— ICommand ScanCommand
        /// <summary>
        /// 只读属性 - 扫码命令
        /// </summary>
        public ICommand ScanCommand
        {
            get => new RelayCommand(x => this.AddBarcode());
        }
        #endregion

        #endregion

        #region 方法

        //Initializations

        #region 加载 —— void Load()
        /// <summary>
        /// 加载
        /// </summary>
        public void Load()
        {
            this.BarcodeNos = new ObservableCollection<string>();
        }
        #endregion


        //Actions

        #region 添加条码 —— void AddBarcode()
        /// <summary>
        /// 添加条码
        /// </summary>
        public void AddBarcode()
        {
            #region # 验证

            if (string.IsNullOrWhiteSpace(this.BarcodeNo))
            {
                MessageBox.Show("物料条码不可为空！", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }

            #endregion

            this.BarcodeNos.Add(this.BarcodeNo);
        }
        #endregion

        #region 删除条码 —— void RemoveBarcode(string barcodeNo)
        /// <summary>
        /// 删除条码
        /// </summary>
        public void RemoveBarcode(string barcodeNo)
        {
            this.BarcodeNos.Remove(barcodeNo);
        }
        #endregion

        #region 提交 —— async void Submit()
        /// <summary>
        /// 提交
        /// </summary>
        public async void Submit()
        {
            #region # 验证

            if (!this.BarcodeNos.Any())
            {
                MessageBox.Show("物料条码不可为空！", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }

            #endregion

            await base.TryCloseAsync(true);
        }
        #endregion

        #endregion
    }
}
